package com.hnx.admin.aurora.service.impl;

import com.hnx.admin.aurora.domain.dto.GenTableColumnDTO;
import com.hnx.admin.aurora.domain.dto.GenTableDTO;
import com.hnx.admin.aurora.domain.query.GenTableQuery;
import com.hnx.admin.aurora.mapper.GenTableMapper;
import com.hnx.admin.aurora.service.GenTableService;
import com.hnx.admin.aurora.util.VelocityInitializer;
import com.hnx.admin.aurora.util.VelocityUtils;
import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.List;
import java.util.Objects;

import static com.hnx.admin.aurora.util.VelocityInitializer.UTF8;

/*
创建人： 倔强的头发
创建时间：2025/6/24
功能描述:

*/
@Service
@RequiredArgsConstructor
public class GenTableServiceImpl implements GenTableService {
    private static final String TPL_SUB = "sub";
    private final GenTableMapper genTableMapper;
    @Override
    public ResultJson selectGenTableList(GenTableQuery genTable) {
        List<GenTableDTO> list = genTableMapper.selectGenTableList(genTable);
        return ResultJson.rows(list);
    }

    @Override
    public ResultJson previewCode(Long tableId) {
        GenTableDTO table = genTableMapper.selectGenTableById(tableId);
        setSubTable(table);
        // 设置主键列信息
        setPkColumn(table);
        VelocityInitializer.initVelocity();
        VelocityContext context = VelocityUtils.prepareContext(table);

        // 获取模板列表
        List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory(), table.getTplWebType());
        for (String template : templates)
        {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, UTF8);
            tpl.merge(context, sw);
//            dataMap.put(template, sw.toString());
        }
        return null;
    }
    public void setSubTable(GenTableDTO table)
    {
        String subTableName = table.getSubTableName();
        if (StringUtils.isNotEmpty(subTableName))
        {
            table.setSubTable(genTableMapper.selectGenTableByName(subTableName));
        }
    }
    /**
     * 设置主键列信息
     *
     * @param table 业务表信息
     */
    public void setPkColumn(GenTableDTO table)
    {
        for (GenTableColumnDTO column : table.getColumns())
        {
            if (column.isPk())
            {
                table.setPkColumn(column);
                break;
            }
        }
        if (Objects.isNull(table.getPkColumn()))
        {
            table.setPkColumn(table.getColumns().get(0));
        }
        if (TPL_SUB.equals(table.getTplCategory()))
        {
            for (GenTableColumnDTO column : table.getSubTable().getColumns())
            {
                if (column.isPk())
                {
                    table.getSubTable().setPkColumn(column);
                    break;
                }
            }
            if (Objects.isNull(table.getSubTable().getPkColumn()))
            {
                table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0));
            }
        }
    }
}
